#!/usr/local/bin/python
# Demian L. Neidetcher

import sys
import cgi

true = 1 
false = 0
global true 
global false

class cgiClass:
	# cgiClass is meant to abstract CGI capabilities even more so than
	# the cgi library included with Python.  The Python library is a 
	# little cumbersome and is not very robust. 
	# One benefit to this object is that it will declare any variables
	# that it recieves from the HTML document, in this way you can
	# literally declare your variables within the HTML code.  Of course
	# you may want to do coding on the server side to adequately use
	# those variables.

	def __init__(self):
		# Pre:	none
		# Post:	the object is created, it also pulls off all of 
		# 			the CGI information.
		import traceback
		sys.stderr = sys.stdout
		print "Content-type: text/html"
		print
		self.keyList = []
		try:
			form = cgi.FieldStorage()
			for key in form.keys():		
				try:
					cmdLine = "self.%s = form['%s'].value"  % (key, key) 
				except AttributeError:
					cmdLine = "self.%s = ''" % (key)
				exec(cmdLine)
				self.keyList.append(key)
		except:
 			print "<html><head><title>Error!</title></head><body>"
		  	print "<pre>"
			traceback.print_exc()
			print form
			print "</pre></body></html>"		

	def get(self, fieldIn):
		# Pre:	takes in a field name
		# Post:	return the value of the field, if the field
		#			does not exist a blank is returned
		for key in self.keyList:
			if fieldIn == key:
				cmdLine = "strOut = self.%s" % (fieldIn)
				exec(cmdLine)
				return strOut
		return '' 

	def setNeededFields(self, listIn):
		# Pre:	takes in a list of fields that the caller 
		#			says must be included for this form
		# Post:	the fields are stored in the object
		self.needList = listIn

	def missingFields(self):
		# Pre:	already have successfully gotten all of the cgi
		#			fields and setNeededFields has been issued
		# Post: 	returns a list of fields that are missing AND
		#			have been specified as necessary 
		listOut = []
		for field in self.needList:
			isIn = false
			for key in self.keyList:
				if field == key: isIn = true 
			if not isIn: listOut.append(field)
		return listOut	
	
	def showVars(self):
		# Pre:	none
		# Post:	shows all of the keys and values
		# Note:	this is mostly for debugging purposes
		for key in self.keyList:
			keyValString = "self.%s" % key
			cmdLine = "print key, %s" % keyValString
			exec(cmdLine)

	def htmlize(self):
		# Pre:	none
		# Post:	returns an html version of all of the variables and values
		#			that those values store
		# Note:	this is mostly useful for debugging but may serve to     
		#			acknowledge what information the user has stored
		strOut = "<center><table border = 1><tr><td>Field</td><td>Value</td></tr>"
		for key in self.keyList:
			cmdLine = "currVal = self.get('%s')" % (key)
			exec(cmdLine)
			currLine = "<tr><td>%s</td><td>%s</td></tr>" % (key, currVal)
			strOut = strOut + currLine + "\n"
		strOut = strOut + "</table></center>"
		return strOut
	
	def __repr__(self):
		# Pre: 	none
		# Post:	the variables are printed to standard out
		# Note:	the __repr__ member function is the default method that is
		#			called by Python when an object is asked to print itself as
		#			in "print object" by default this would produce a more generic
		#			representation like <object at address:0x13223> or something
		#			like that.  here we over-ride it to serve our purposes.
		return self.showVars()

if __name__ == "__main__":
	# Pre:	none
	# Post:	the object is declared and shows all of its values
	# Note:	this is just for testing/ debugging
	form = cgiClass()
	form.showVars()
	
